﻿/*******************************************************
 *  课件管理 相关操作js
 * <p>Title: docManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2019年8月5日 下午5:41:57
 * @version 1.0
 * *****************************************************/
var $table, unitzTree, options,//树控件
    selNode = null,//选中节点
    indexForm,
    checkNodeSpan = null;//选中树id;
var headManager = {
    //1.0 初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        headManager.initSelect();
        headManager.bindZtree();
        headManager.bindEvent();
        headManager.initTable();
        parent.delLoadBoxs();
    },
    //1.1 采用Ztree初始化文档树
    bindZtree: function () {
        var setting = {
            view: {
                showIcon: true,
                selectedMulti: false,
                fontCss: getFontCss
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                dataType: "text",
                type: "post",
                url: ctx + "/DocManager/GetDocTreeData?pro_type=3&notType=5", //notType=5没有实际意义，大于0表示需要排除审核不通过的
                autoParam: ["id"]
            },
            data: {
                key: { title: "name", name: "name" },
                simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 }
            },
            check: {
                enable: false,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "p", "N": "ps" }
            },
            callback: {
                onAsyncError: function () {
                    layer.msg("初始化单位结构树失败~", { icon: 5 });
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    //树加载完成后执行
                    var obj = unitzTree.getNodes()[0];
                    if (obj) {
                        unitzTree.selectNode(obj);
                        unitzTree.setting.callback.onClick(null, unitzTree.setting.treeId, obj);//模拟点击事件 
                        if (obj.children && obj.children.length > 0)
                            unitzTree.expandNode(obj);
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    $('#unitTree a').css("color", "#333");
                    $('#' + treeNode.tId + '_a').css("color", "#ffffff");
                    checkNodeSpan = treeNode.tId + '_span';
                    selNode = treeNode;//选中节点
                    if ($table && treeNode.id > 0)
                        $table.bootstrapTable('refresh', options);//刷新Table
                }
            }
        };
        unitzTree = $("#unitTree").initzTreeView(setting, 'ushow');
    },
    //1.2 初始化Table
    initTable: function () {
        options = {
            url: ctx + "/HeadManager/InitTable",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            singleSelect: false,//多选
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    keyword: $('#keyword').val(),
                    folder_id: (selNode)?selNode.id:0,
                    folder_path: (selNode) ? selNode.folder_path : "",
                    fileType:$('#fileType').val(),
                    fileClassify: $('#fileClassify').val(),
                    fileMoney:$('#fileMoney').val()
                }
                return params;
            },
            columns:[
            {
                checkbox: true
            },{
                field: '',
                title: '序号',
                align: 'center',
                width: 120,
                formatter: function (value, row, index) {                     
                    return index + 1;
                }
            }, {
                field: 'file_note',
                title: '缩略图',
                align: 'center',
                formatter: function (value, row, index) {
                    return "<a href='#' onclick=headManager.openPicture(" + row.file_id + ") class=\"btn btn-primary btn-circle\" title=\"缩略图\"><i class=\"fa fa-picture-o\"></i></a>";
                }
            }, {
                field: 'file_name',
                title: '课件标题',
                align: 'left',
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'file_code',
                title: '代码',
                align: 'center'
            }, {
                field: 'folder_author',
                title: '作者',
                align: 'center'
            }, {
                field: 'pro_type_text',
                title: '产品类别',
                align: 'center'
                //formatter: function (value, row, index) {
                //    switch (value) {
                //        case 1: return "VRPPT"; break;
                //        case 2: return "创客"; break;
                //        case 3: return "头记"; break;
                //        case 4: return "校史馆"; break;
                //        default: return "-"; break;
                //    }
                //}
            }, {
                field: 'file_classify',
                title: '适合分类',
                align: 'center'
            }, {
                field: 'browse_num',
                title: '浏览',
                align: 'center'
            }, {
                field: 'vr_star',
                title: '点赞',
                align: 'center'
            }, {
                field: 'down_number',
                title: '下载',
                align: 'center'
            }, {
                field: 'file_status',
                title: '属性',
                align: 'center',
                formatter: function (value, row, index) {
                    switch (value) {
                        case 0: return "<span class='label label-primary'>私有</span>"; break;
                        case 1: return "<span class='label label-warning'>待审核</span>"; break;
                        case 5: return "<span class='label label-success'>已上架</span>"; break;
                        case 4: return "<span class='label label-danger'>已下架</span>"; break;
                        case 6: return "<span class='label label-danger'>审核未通过</span>"; break;
                    }
                }
            }, {
                field: 'folder_limit',
                title: '权限',
                align: 'center'
            }, {
                field: 'approve_time',
                title: '审核通过时间',
                align: 'center'
            }, {
                field: 'update_time',
                title: '最后修改时间',
                align: 'center'
            }],
            onClickRow: function (row, $element) {
            },
            onLoadSuccess: function (data) {
                var val = "";
                if (checkNodeSpan)
                    val = $('#' + checkNodeSpan).text();
                if (data && data.total > 0 && checkNodeSpan) {
                    if (val.indexOf('(') > -1)
                        $('#' + checkNodeSpan).text(val.split('(')[0] + "(" + data.total + ")");
                    else
                        $('#' + checkNodeSpan).text(val + "(" + data.total + ")");
                } else if (val.indexOf('(') > -1 && checkNodeSpan)
                    $('#' + checkNodeSpan).text(val.split('(')[0]);
            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    initSelect:function(){
        $('#fileClassify').initSelectpicker("docClassify", null, true);
    },
    //1.3 事件绑定
    bindEvent: function () {
        //查询
        $('#btnSearch').on('click',function () {
            var zTree = headManager.getSelNode();
            if (!zTree) {
                layer.msg("温馨提示：查询前请先选着目录节点！", { icon: 1 });
                return null;
            }
            $table.bootstrapTable('refresh', options);
        });
        //编辑
        $('#btnEditFile').on('click', function () {
            var row = headManager.selections();
            if (!row)
                return false;
            indexForm = layer.open({
                id: 'ifreamUpLoadData',
                title: ['编辑', 'font-size:18px;'],
                type: 2,
                content: ctx + '/SourceManager/EntryFile/' + row.file_id + '?file_id=' + row.file_id + '&isHeader=ok&pro_type=3&pageType=&doc_type=',
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['确定', '取消'],
                area: ['98%', '95%'],
                yes: function (li, o) {
                    var result = document.getElementById("ifreamUpLoadData").firstChild.contentWindow.entryManager.saveForm();
                    $table.bootstrapTable('refresh', options);
                    if (result)
                        layer.closeAll();
                },
                cancel: function (li, o) {

                }
            });
        });
        //删除
        $('#btnDeleteFile').on('click', function () {

        });
    },
    //1.4 获取被选中节点
    getSelNode: function () {
        //获取页面doc_type参数
        //获取节点folder_id参数
        var zTree = $.fn.zTree.getZTreeObj("unitTreezTreeData");
        if (!zTree)
            return null;
        var nodes = zTree.getSelectedNodes(),
           treeNode = nodes[0];
        return treeNode;
    },
    //1.5 获取被选中节点
    selections: function (isAll) {
        var row = $table.bootstrapTable('getSelections');
        if (!row || row.length === 0) {
            layer.msg("数据为空或未选中记录行~", { icon: 5 });
            return false;
        }
        if (isAll)
            return row;
        if (row.length > 1) {
            layer.msg("当前操作不支持多选，请选择一行再操作！", { icon: 5 });
            return false;
        }
        return row[0];
    },
    //1.6 缩略图
    openPicture: function (file_id) {
        var page = " <div id='createPicturePage' style='overflow-x: hidden;overflow-y:auto;margin-top:10px;margin-right:10px; display: none;'>";
        page += "<div class='form-group' style='position:absolute;right:30px;color:red;'><i class='fa fa-info-circle'></i>&nbsp;双击鼠标左键选择删除！ <button id='btnEditFile' onclick='docManager.openPicUpPage(" + file_id + ")' class='btn btn-primary' type='button'><i class='fa fa-pencil'></i>&nbsp;上传缩略图</button></div><div id='showPictureList' class='form-group' style='margin:20px 5px 15px;'>";
        //获取数据
        $.ajax({
            type: 'post',
            url: ctx + '/SourceManager/GetPicUrlByID/' + file_id,
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data.Statu === 0) {
                    for (var i = 0; i < data.Data.length; i++)
                        page += "<div id='img_" + data.Data[i].bag_id + "' style='width:280px;height:320px;margin-top:20px;margin-left:20px;cursor:pointer;float:left;text-align:center;'><img src=" + data.Data[i].file_url.toString().replace("~", window.location.origin + $.ctx()) + " title='双击鼠标左键允许删除当前缩略图！' ondblclick=docManager.delPicFileById('" + data.Data[i].file_name + "'," + data.Data[i].bag_id + ") style='width:100%;height:280px;border:1px solid #B2B2B2;border-radius:5px;'/><span>" + data.Data[i].file_name + "</span></div>";
                }
                else
                    layer.msg(data.Msg, { icon: 5 });
            }
        });
        var pic = $('#createPicturePage');
        if (pic)
            pic.remove();
        //添加图片
        page += "</div></div>";
        $('body').append($(page));
        picForm = layer.open({
            title: ['缩略图', 'font-size:18px;'],
            type: 1,
            content: $('#createPicturePage'),
            scroll: true,//是否显示滚动条、默认不显示
            btn: '',
            area: ['700px', '90%'],
            yes: function (li, o) { },
            cancel: function (li, o) { }
        });
    },
    //7.4 删除缩略图、目录树图标
    delPicFileById: function (bagName, bagId, picType) {
        layer.confirm('您确定要删除：' + bagName + " 图片吗？", {
            btn: ['确定', '取消'] //按钮
        }, function () {
            var layerIndex = layer.msg('数据处理中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
            $.ajax({
                type: 'post',
                url: ctx + '/SourceManager/DelPicUrlByID/' + bagId,
                dataType: 'json',
                data: { picType: picType },
                async: false,
                success: function (data) {
                    layer.close(layerIndex);
                    if (data.Statu === 0) {
                        layer.msg(data.Msg, { icon: 6 });
                        $('#img_' + bagId).remove();
                    }
                    else
                        layer.msg(data.Msg, { icon: 5 });
                }
            });
        }, function () { });
    }
}